package labox.innovation.gameserver.model.quest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import labox.innovation.config.Config;
import labox.innovation.gameserver.model.actor.L2Npc;
import labox.innovation.gameserver.model.actor.instance.FuzePcInstance;
import labox.innovation.gameserver.timer.Timer;

public class QuestTimer {
	protected static final Logger _log = LoggerFactory.getLogger(QuestTimer.class.getName());

	// =========================================================
	// Data Field
	private boolean _isActive = true;

	private String _name;

	private Quest _quest;

	private L2Npc _npc;

	private FuzePcInstance _player;

	private boolean _isRepeating;

	private Timer _schedular;

	// =========================================================
	// Constructor
	public QuestTimer(Quest quest, String name, int time, L2Npc npc, FuzePcInstance player, boolean repeating) {
		_name = name;
		_quest = quest;
		_player = player;
		_npc = npc;
		_isRepeating = repeating;
		_schedular=new Timer(time,repeating,new Runnable(){
			public void run() {
				if (!getIsActive())
					return;
				try {
					if (!getIsRepeating())
						cancel();
					getQuest().notifyEvent(getName(), getNpc(), getPlayer());
				} catch (Exception e) {
					_log.error("new QuestTimer schedular", Config.SERVER_ID, e);
				}
			}
		}
		);
		_schedular.start();
	}

	
	
	
	public QuestTimer(Quest quest, String name, int time, L2Npc npc, FuzePcInstance player) {
		this(quest, name, time, npc, player, false);
	}

	public QuestTimer(QuestState qs, String name, int time) {
		this(qs.getQuest(), name, time, null, qs.getPlayer(), false);
	}

	// =========================================================
	// Method - Public
	public void cancel() {
		_isActive = false;

		if (_schedular != null)
			_schedular.cancel();

		getQuest().removeQuestTimer(this);
	}

	/**
	 * public method to compare if this timer matches with the key attributes passed.
	 * @param quest : Quest instance to which the timer is attached
	 * @param name : Name of the timer
	 * @param npc : Npc instance attached to the desired timer (null if no npc attached)
	 * @param player : Player instance attached to the desired timer (null if no player attached)
	 */
	public boolean isMatch(Quest quest, String name, L2Npc npc, FuzePcInstance player) {
		if ((quest == null) || (name == null))
			return false;
		if ((quest != getQuest()) || name.compareToIgnoreCase(getName()) != 0)
			return false;
		return ((npc == getNpc()) && (player == getPlayer()));
	}

	// =========================================================
	// Property - Public
	public final boolean getIsActive() {
		return _isActive;
	}

	public final boolean getIsRepeating() {
		return _isRepeating;
	}

	public final Quest getQuest() {
		return _quest;
	}

	public final String getName() {
		return _name;
	}

	public final L2Npc getNpc() {
		return _npc;
	}

	public final FuzePcInstance getPlayer() {
		return _player;
	}

	@Override
	public final String toString() {
		return _name;
	}
}
